function waxSign(){
    var clickApprove = function(){
        var approve = $("div:contains(Approve)");
        console.log("approve found:"+approve.length);
        if(approve.length>0){
            console.log("approve clicking");
            $(approve[approve.length-1]).parents("button").click();
            setTimeout(clickApprove,10000);
        }else{
            setTimeout(clickApprove,1000);
        }
     }
     setTimeout(clickApprove,1000);
}

function checkLogin(){
    if($("p:contains(Trilium Balance)").length==0){
        location.reload();
    }
}

function alienLogin(){
    var startNow = $("span:contains(Start Now)");
    if(startNow.length>0){
        startNow[0].click();
        console.log('click start now')
        setTimeout(checkLogin, 360000)
    }
}

function getCpuPercent(){
	var resp = $.ajax({
		type : "post",
		dataType : "json",
		async: false,
		url : 'https://chain.wax.io/v1/chain/get_account',
		contentType : 'application/json;charset=utf-8',
	    data  : JSON.stringify({"account_name":"zghya.wam"})
	});
	var limit = JSON.parse(resp['responseText'])['cpu_limit'];
	var percent = limit['used']/limit['max'];
	return percent;
}

function alienMine(){
    console.log("alien mine v0.2");
    var waitOrMine = function(){
        var wait = $("p:contains(Next Mine Attempt)").length;
        if(wait>0){
            console.log("**************wait**************");
            return;
        };
        var claim = $("span:contains(Claim Mine)");
        if(claim.length>0){
            return;
        }
        var mine = $("span:contains(Mine)");
        if(mine.length>0){
            console.log("**************mine click**************");
            mine[0].click();
        }
    }
    var claim = function(){
        var claim = $("span:contains(Claim Mine)");
        if(claim.length>0){
            console.log("**************claim**************");
            claim[0].click();
            setTimeout("window.focus()", 2000);
            return;
        }
    }
    setInterval(waitOrMine,3000);
    setInterval(claim,60000);
    setTimeout("location.reload()",360*60*1000)
}

function wax_alien_main(){
    if(window.location.href.indexOf('all-access.wax.io')>0){
        waxSign();
    }else if(window.location.href.indexOf('play.alienworlds.io')>0){
        alienLogin();
        setTimeout(alienMine,30000);
    }
}